/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Parser;

import Entities.Header;
import Entities.Mail;
import java.util.Date;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author fabio
 */
public class HeaderParser {

    //@TODO tolower() values
    MimeMessage mimeMessage;
    Mail mail;

    HeaderParser(Mail mail, MimeMessage mimeMessage) {
        this.mail = mail;
        this.mimeMessage = mimeMessage;
    }

    public Header getHeader() throws MessagingException {
        //From
        Address from = mimeMessage.getFrom()[0];
        String fromMailAddress = getNulls(getMailAddress(from));
        String fromMailDomain = getNulls(getMailDomain(fromMailAddress));
        String fromMailTopDomain = getNulls(getMailTopDomain(fromMailDomain));
        String fromMailAuthor = getNulls(getMailAuthor(from));
        //Sender
        Address sender = mimeMessage.getSender();
        String senderMailAddress = getNulls(getMailAddress(sender));
        String senderMailDomain = getNulls(getMailDomain(senderMailAddress));
        String senderMailTopDomain = getNulls(getMailTopDomain(senderMailDomain));
        String senderMailAuthor = getNulls(getMailAuthor(sender));
        //Reply-to
        Address replyTo = mimeMessage.getReplyTo()[0];
        String replyToMailAddress = getNulls(getMailAddress(replyTo));
        String replyToMailDomain = getNulls(getMailDomain(replyToMailAddress));
        String replyToMailTopDomain = getNulls(getMailTopDomain(replyToMailDomain));
        String replyToMailAuthor = getNulls(getMailAuthor(replyTo));
        //Return-Path
        String returnPath = getReturnPath(mimeMessage);
        String returnPathMailAddress = getNulls(getReturnPathMail(returnPath));
        String returnPathMailDomain = getNulls(getMailDomain(returnPathMailAddress));
        String returnPathMailTopDomain = getNulls(getMailTopDomain(returnPathMailDomain));
        //Subject
        String subject = getNulls(getNulls(mimeMessage.getSubject()).replaceAll("[^a-zA-Z ]", ""));
        //Date
        Date sentDate = mimeMessage.getSentDate();
        return new Header(mail, fromMailAddress, fromMailDomain, fromMailTopDomain, fromMailAuthor, senderMailAddress, senderMailDomain, senderMailTopDomain, senderMailAuthor, replyToMailAddress, replyToMailDomain, replyToMailTopDomain, replyToMailAuthor, returnPathMailAddress, returnPathMailDomain, returnPathMailTopDomain, sentDate, subject);

    }

    private String getNulls(String string) {
        if (string == null || string.equals("")) {
            return "null";
        } else {
            string = string.toLowerCase();
        }
        return string;
    }

    private String getMailAuthor(Address address) {
        if (address == null) {
            return "";
        }
        String string = address.toString();
        int start = 0;
        int end = string.indexOf('<');
        if (start != -1 && end != -1 && end > 0) {
            return string.substring(start, end - 1);
        } else {
            return "";
        }
    }

    private String getMailAddress(Address address) {
        if (address == null) {
            return "";
        }
        String string = address.toString();
        int start = string.lastIndexOf('<');
        int end = string.lastIndexOf('>');
        if (start != -1 && end != -1) {
            return string.substring(start + 1, end);
        } else {
            return string;
        }
    }

    private String getMailDomain(String string) {
        if (string.isEmpty() || string == null) {
            return "";
        }
        int start = string.lastIndexOf('@') + 1;
        return string.substring(start);
    }

    private String getMailTopDomain(String string) {
        if (string == null || string.equals("")) {
            return "";
        }
        int start = string.lastIndexOf('.');
        if (start != -1) {
            return string.substring(start + 1);
        } else {
            return "";
        }
    }

    private String getReturnPath(MimeMessage mimeMessage) throws MessagingException {
        if (mimeMessage.getHeader("Return-Path") != null) {
            return mimeMessage.getHeader("Return-Path")[0].toString();
        } else {
            return "";
        }
    }

    private String getReturnPathMail(String string) {
        if (string == null || string.equals("")) {
            return "";
        }
        int start = string.lastIndexOf('<');
        int end = string.lastIndexOf('>');
        if (start != -1) {
            return string.substring(start + 1, end);
        } else {
            return "";
        }
    }
}
